package service

import (
	"context"

	"gitee.com/linxing_3/sr-core/domain/service/srvctx"
	"gitee.com/mkwchecking/sr-admin/apps/assets/acl"
	"gitee.com/mkwchecking/sr-admin/apps/assets/repo"
	"gitee.com/mkwchecking/sr-admin/pkg/file/scanner"
)

func NewAssetsScanner(file *repo.AssetsFile, dir *repo.AssetsDir) *AssetsDirScanner {
	s := &AssetsDirScanner{
		scanner: scanner.NewScanner(
			repo.NewAssetScanHandler(file, dir),
		),
	}

	go s.scanner.Start(context.Background())
	return s
}

// 文件目录扫描器
type AssetsDirScanner struct {
	scanner scanner.IScanner
}

func (s *AssetsDirScanner) ScanDir(ctx srvctx.IContext, req *acl.AssetsLocalScanReq) error {
	err := s.scanner.Scan(
		ctx,
		scanner.WithExts(req.Exts),
		scanner.WithExcludeExts(req.ExcludeDirs),
		scanner.WithIgnoreHidden(req.IgnoreHidden),
		scanner.WithPaths(req.Dir),
		scanner.WithAsync(),
	)

	return err
}

// 获取扫描状态
func (s *AssetsDirScanner) GetScanState(ctx context.Context) []acl.AssetsDirScanState {
	states := s.scanner.GetJobStates()
	ret := make([]acl.AssetsDirScanState, 0, len(states))
	for _, state := range states {
		ret = append(ret, acl.AssetsDirScanState{
			ScanState: *state,
		})
	}

	return ret
}
